﻿using HuaCheng.CommonClass;
using HuaCheng.DataAccess;
using HuaCheng.UserControls;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Controller
{
    /// <summary>
    /// 通道映射control层
    /// </summary>
    public class ChannelMapControl
    {
        /// <summary>
        /// 添加自定义列
        /// </summary>
        /// <param name="dgv"></param>
        public static void AddDgvColumn(DataGridView dgv)
        {
            //第一列
            dgv.AutoGenerateColumns = false;
            TextAndImageColumn ColumnChlId = new TextAndImageColumn();//自定义列类型为文字+图片
            ColumnChlId.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            ColumnChlId.Name = "chlID";//列名
            ColumnChlId.HeaderText = ResourceCulture.GetString("ChlID");//列标题
            ColumnChlId.Width = 100;//列宽
            dgv.Columns.Add(ColumnChlId);
            ColumnChlId.ReadOnly = true;
            //第二列
            dgv.AutoGenerateColumns = false;
            TextAndImageColumn ColumnChlType = new TextAndImageColumn();
            ColumnChlType.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            ColumnChlType.Name = "chlType";
            ColumnChlType.HeaderText = ResourceCulture.GetString("ChannelType");
            ColumnChlType.Width = 150;
            ColumnChlType.ReadOnly = true;
            dgv.Columns.Add(ColumnChlType);
        }
        /// <summary>
        /// 删除通道映射
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteDev(string ip,int gid,int mid)
        {
            ChlMapHelper.DeleteDevMapById(ip,gid,mid);//删除原有的通道映射信息
        }
        /// <summary>
        /// 修改通道映射
        /// </summary>
        /// <param name="devUid"></param>
        /// <param name="chs"></param>
        public static void ModifyDevMap(string ip,int gid,int mid, ChannelSimple chs, string auxNums)
        {
            BTSChlMap bts = new BTSChlMap()
            {
                DevIp = ip,//IP地址
                Gid = gid,//柜号
                Mid = mid,//中位机号
                ChlMapId = chs.ChlMapId,//映射通道号
                ChlRealId = chs.ChlRealId,//实际通道号
                RevocationStatus = !chs.Visible,//撤销状态与可视性相反
                StrAuxNums = auxNums,
            };
            ChlMapHelper.ModifyDevMap(bts);//插入当前设置的映射信息
        }
        /// <summary>
        /// 查询是否存在主通道映射
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="chs"></param>
        /// <returns></returns>
        public static bool queryMap(string ip,ChannelSimple chs)
        {
            BTSChlMap bts = new BTSChlMap()
            {
                DevIp = ip,//IP
                Gid = chs.GID,//柜号
                Mid = chs.MID,//中位机号
                ChlMapId = chs.ChlMapId,//映射通道号
                ChlRealId = chs.ChlRealId,//实际通道号
                RevocationStatus = !chs.Visible,//撤销状态与可视性相反
            };
            return ChlMapHelper.queryMap(bts);
        }
        /// <summary>
        /// 映射辅助通道
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="chs"></param>
        /// <param name="auxNums"></param>
        public static void ModifyAuxMap(string ip, ChannelSimple chs, string auxNums)
        {
            BTSChlMap bts = new BTSChlMap()
            {
                DevIp = ip,//柜号
                Gid = chs.GID,//柜号
                Mid = chs.MID,//中位机号
                ChlMapId = chs.ChlMapId,//映射通道号
                ChlRealId = chs.ChlRealId,//实际通道号
                StrAuxNums = auxNums,
            };
            ChlMapHelper.ModifyAuxMap(bts);//插入当前设置的映射信息
        }
        /// <summary>
        /// 查询通道映射
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<BTSChlMap> GetDevChlMap(string ip,int gid,int mid)
        {
            return ChlMapHelper.GetDevChlMap(ip,gid,mid);//获取通道映射表
        }
        /// <summary>
        /// 查询每行通道映射个数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetDevChlMapLine(string ip,int gid,int mid)
        {
            List<int> data = (List<int>)ChlMapHelper.QueryMapRowAndCol(ip,gid,mid);//查询数据库得数据表行和列
            return data[0];
        }
        public static List<BTSChlMap> GetChlPalallelInfo(string devIp, int gid,int mid,Hashtable parallel)
        {
            List<BTSChlMap> btsChls = ChlMapHelper.GetChlPalallelInfo(devIp,gid,mid);//查询映射及并联信息
            foreach (BTSChlMap bts in btsChls)
            {
                if (bts.DevParallelGroup >= 0)
                {
                    int parallelGroup = bts.DevParallelGroup;

                    int chl = bts.ChlRealId;

                    if (!parallel.ContainsKey(parallelGroup))//如果key未添加到哈希表则添加key-value
                    {
                        List<int> ls = new List<int>();
                        ls.Add(chl);
                        parallel.Add(parallelGroup, ls);
                    }
                    else
                    {
                        ((List<int>)parallel[parallelGroup]).Add(chl);//如果key已存在则给value加值
                    }
                }
            }
            return btsChls;
        }
    }
}
